package com.company.base.utils;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;

/**
 * 反转图片
 * 
 * @author kakk
 *
 */
public class ReverseImage {
	public static void main(String[] args) throws Exception {
		//checkReverse("D:/2.jpg",90,null);
	}
	/**
	 * 判断是否横拍。否则顺时针旋转90°
	 * @param src
	 * @param degree
	 * @throws Exception
	 * @throws IOException
	 */
	public static Boolean checkReverse(InputStream srcFileStream,int degree,Boolean isHorizontal) throws Exception, IOException {
		try {
			System.out.println("进入checkReverse");
			//File imageFile = new File();
			Metadata metadata = ImageMetadataReader.readMetadata(srcFileStream);
			for (Directory directory : metadata.getDirectories()) {
				System.out.println("进入directory");
				for (Tag tag : directory.getTags()) {
					System.out.println("进入tag："+tag.getTagName());
					if ("Orientation".equals(tag.getTagName())) {
						System.out.println("图片Orientation:"
								+ tag.getDescription());
						if ("Right side, top (Rotate 90 CW)".equals(tag
								.getDescription())) {
							System.out.println("进入反转");
							//BufferedImage image = ImageIO.read(new FileInputStream(imageFile));
							//rotateImage(src,image, 90,false);
							//Image src = ImageIO.read(srcFileStream);
							return true;
						}
					}
				}
			}
		} catch (ImageProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 旋转任意度数
	 * @param bufferedimage
	 * @param degree
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static BufferedImage rotateImage(String src,BufferedImage bufferedimage,int degree,Boolean isHorizontal) throws FileNotFoundException, IOException {
		int w = bufferedimage.getWidth();
		int h = bufferedimage.getHeight();
		//int type = bufferedimage.getColorModel().getTransparency();
		BufferedImage img;
		Graphics2D graphics2d;
		if(isHorizontal){
			(graphics2d = (img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB)).createGraphics())
			.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
					RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
		}else {
			(graphics2d = (img = new BufferedImage(h, w, BufferedImage.TYPE_INT_ARGB)).createGraphics())
			.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
					RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			graphics2d.rotate(Math.toRadians(degree), (w / 2)-(w-h)/2, h / 2);
		}
		graphics2d.drawImage(bufferedimage,null,null);
		graphics2d.dispose();
		ImageIO.write(img, "jpg", new FileOutputStream((src.split("\\."))[0]+".jpg"));
		if(!isHorizontal){
			//竖->横拍
			ConvertJPEG.writeJPEG(new File((src.split("\\."))[0]+".jpg"), new File(src), 100);
			//RenderedImage imgPng = ImageIO.read(new File((src.split("\\."))[0]+".png"));   
			//ImageIO.write(imgPng, "jpg", new FileOutputStream(src));
		}
		return img;
	}
}
